package com.yuexun.dhdoctor.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;


import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.config.ScreenUtil;
import com.yuexun.dhdoctor.util.EnvironmentShare;

import org.kymjs.kjframe.ui.ViewInject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Administrator on 2017/5/24 0024.
 */

public class WriteDialog {

    private Context context;
    private ICallBack icb;
    private Dialog dialog;
    private Display display;

    private Paint paint;
    private Bitmap baseBitmap;
    private Canvas canvas;

    ImageView writeView;
    TextView viewCancel;
    TextView viewUploading;

    public WriteDialog(Context context){
        this.context = context;

        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public WriteDialog builder() {
        // 获取Dialog布局
        View view = LayoutInflater.from(context).inflate(
                R.layout.dialog_write_view, null);
        writeView = (ImageView) view.findViewById(R.id.write_view);

        viewCancel = (TextView) view.findViewById(R.id.view_cancel);
        viewUploading = (TextView) view.findViewById(R.id.view_uploading);

        writePaint(writeView);
        viewCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        viewUploading.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (writeView.getDrawable() == null) {
                    ViewInject.toast("请先画图");
                } else {
                    save();
                }
            }
        });

        // 设置Dialog最小宽度为屏幕宽度
        view.setMinimumWidth(display.getWidth());

        dialog = new Dialog(context, R.style.dialog4style);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(true);
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = ScreenUtil.getScreenWidth(context);
        params.gravity = Gravity.BOTTOM;
        dialog.getWindow().setAttributes(params);
        dialog.show();
        return this;

    }

    public void dismiss(){
        dialog.dismiss();
    }

    public void save() {
        if (EnvironmentShare.haveSdCard()) {
            try {
                File tempFile = File.createTempFile("write_", ".jpg", EnvironmentShare.getWriteViewDir());
                FileOutputStream stream = new FileOutputStream(tempFile);
                baseBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                stream.close();
                if(icb != null){
                    // 这个方法在传入的回调器中实现
                    icb.callback(tempFile);
                }
                dialog.dismiss();
            } catch (IOException e) {
                ViewInject.toast("保存图片失败");
                e.printStackTrace();
            }

        } else {
            ViewInject.toast("请插入SD卡");
        }
    }

    private void writePaint(final ImageView wView) {
        paint = new Paint();
        paint.setStrokeWidth(5);
        paint.setColor(Color.RED);
        // 创建一个可以被修改的bitmap
        baseBitmap = Bitmap.createBitmap(ScreenUtil.getScreenWidth(context) - 30, ScreenUtil.dip2px(context, 220),
                Bitmap.Config.ARGB_8888);
        canvas = new Canvas(baseBitmap);
        canvas.drawColor(Color.WHITE);
        // 知道用户手指在屏幕上移动的轨迹
        wView.setOnTouchListener(new View.OnTouchListener() {
            // 设置手指开始的坐标
            int startX;
            int startY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: // 手指第一次接触屏幕
                        startX = (int) event.getX();
                        startY = (int) event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:// 手指在屏幕上滑动
                        int newX = (int) event.getX();
                        int newY = (int) event.getY();

                        canvas.drawLine(startX, startY, newX, newY, paint);
                        // 重新更新画笔的开始位置
                        startX = newX;
                        startY = newY;
                        wView.setImageBitmap(baseBitmap);
                        break;
                    case MotionEvent.ACTION_UP: // 手指离开屏幕
                        break;

                    default:
                        break;
                }
                return true;
            }
        });
    }

    public void setCallBack (ICallBack cb) {
        icb = cb;
    }

    public interface ICallBack {
        void callback(File tempFile);
    }
}
